import { Message, MessageBox } from 'element-ui'
import axios from 'axios'

// 创建axios实例
const service = axios.create({
  baseURL: process.env.BASE_API, // api的base_url
  timeout: 15000 // 请求超时时间
})
export default {
  name: 'home',
  data() {
    return {
      baseURL: process.env.BASE_API,
      tableSize:"",
      length:0,
      addArr: [],
      listLoading: false,
      tableData: [],
      pagination: {
        page: 1,
        pageSize: 1,
        total: 0,
      },
      totalPage: 1,
      countTotal: 2000,
      selectedIds:[],
      checked:false,
      otherHeight:0,
      pageHeight:0,
    }
  },
  props:{

  },
  methods: {
    downFile(){
      window.location.href = this.baseURL+'/readExcel/export';
    },
    submitAddDown() {
      this.submitAddFile(this.downFile);
    },
    submitAddFile(downFunction) {
      if (0 == this.addArr.length) {
        this.$message({
          type: 'info',
          message: '请选择要上传的文件'
        });
        return;
      }

      var formData = new FormData();
      formData.append('num', this.addType);
      formData.append('linkId', this.addId);
      formData.append('filename', this.addFileName);
      for (var i = 0; i < this.addArr.length; i++) {
        formData.append('file', this.addArr[i]);
      }
      let config = {
        headers: {
          'Content-Type': 'multipart/form-data',
          'Authorization': this.token
        }
      };
      service.post('/readExcel/importZclist', formData, config)
        .then((response) => {
          if (response.data.code == 200) {
            Message({
              message: '附件上传成功!',
              type: 'success',
              duration: 3 * 1000

            });
            this.resetAdd();
            this.tableData = response.data.data;
            this.tableSize = "当前导入数据"+this.tableData.length+"条";
            downFunction();
          }else{
            Message({
              message: '附件上传失败!',
              type: 'error',
              duration: 3 * 1000

            });
          }
        })
    },
    getFile(event) {
      var file = event.target.files;
      for (var i = 0; i < file.length; i++) {
        //    上传类型判断
        var imgName = file[i].name;
        var idx = imgName.lastIndexOf(".");
        if (idx != -1) {
          var ext = imgName.substr(idx + 1).toUpperCase();
          ext = ext.toLowerCase();
          if (ext != 'xls' && ext != 'xlsx') {
            Message({
              message: '请上传正确的Excel文件!',
              type: 'error',
              duration: 3 * 1000
            });
          }
          // if(this.addArr.length > 0) {
          //   Message({
          //     message: '一次只能上传一个文件!',
          //     type: 'error',
          //     duration: 3 * 1000
          //   });
          this.addArr = [];
          this.addArr.push(file[i]);
        }
      }
    },
    clearFile() {
      this.$refs.clearFile.value = ''; // 清空file文件
    },
    //清除上传的文件方法
    resetAdd(){
      this.$refs.clearFile.value = ''; // 清空file文件
      this.addFileName = "";
      this.addArr =[];
    },
    sortChange(column) {
      //简单倒序
      this.tableData = this.tableData.reverse();
    }
  }
}
